package physgin;

import java.util.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.image.*;
import java.awt.Container;

/**
 * A ResourceaCache for J3D textures.
 * Caches previously used textures.
 */
public class J3dTextureCache implements ResourceCache<String, Texture>
{ 
    private Map<String, Texture> tx;
    
    /**
     * Default class constructor.
     */
    J3dTextureCache()
    {
        tx = new HashMap<String, Texture>();
    }
    
    /**
     * Load a texture resource via this method to cache its data.
     * Thus speeding up future accesses.
     *
     * @param   f   String of the textures location.
     * @return      Texture requested.
     */
    public Texture loadResource(String f)
    {
        Texture t;
        
        if (!tx.containsKey(f))
        {
            TextureLoader loader = new TextureLoader(f, "RGB", new Container());
            t = loader.getTexture();
            t.setBoundaryModeS(Texture.WRAP);
            t.setBoundaryModeT(Texture.WRAP);
            tx.put(f, t);
        }
        else
            t = tx.get(f);
        
        return t;
    }

}
